﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace NetTopologySuite.GeometriesGraph.Index
{
	public class MonotoneChainIndexer
	{
		[Obsolete("Use List<int>.ToArray()")]
		public static int[] ToIntArray(IList<int> list)
		{
			int[] array = new int[list.Count];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = Convert.ToInt32(list[i]);
			}
			return array;
		}

		public int[] GetChainStartIndices(Coordinate[] pts)
		{
			int num = 0;
			List<int> list = new List<int>();
			list.Add(num);
			do
			{
				int num2 = MonotoneChainIndexer.FindChainEnd(pts, num);
				list.Add(num2);
				num = num2;
			}
			while (num < pts.Length - 1);
			return list.ToArray();
		}

		private static int FindChainEnd(Coordinate[] pts, int start)
		{
			int num = QuadrantOp.Quadrant(pts[start], pts[start + 1]);
			int num2 = start + 1;
			while (num2 < pts.Length && QuadrantOp.Quadrant(pts[num2 - 1], pts[num2]) == num)
			{
				num2++;
			}
			return num2 - 1;
		}
	}
}
